﻿using Microsoft.AspNetCore.Mvc;
using MyWeb.JwtAuthorization.Services;

namespace MyWeb.JwtAuthorization.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class JwtController : ControllerBase
    {
        private IJwtService _IJWTService = null;
        public JwtController(IJwtService iJWTService)
        {
            this._IJWTService = iJWTService;
        }

        /// <summary>
        /// 校验账号密码--生成JWT Token---更多都是框架已经封装
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetToken(string name = "eleven", string password = "123456")
        {
            Console.WriteLine($"{name}--{password}");

            if ("eleven".Equals(name) & "123456".Equals(password))
            {
                //生成JWT 
                string token = this._IJWTService.GetToken(name);

                return new JsonResult(new
                {
                    Result = true,
                    Token = token
                });
            }
            else
            {
                return new JsonResult(new
                {
                    Result = false,
                    Token = " "
                });
            }

        }
    }
}
